package com.wy.cms.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wy.domain.CmsResource;
import com.wy.service.ICmsResourceService;
import com.wy.service.ex.ICmsResourceExService;
import org.springframework.beans.factory.annotation.Autowired;

public class ResourcesUtil {
	
	private static Map<Long,Map<Long,Map<Long,CmsResource>>> resources = new HashMap<Long, Map<Long,Map<Long,CmsResource>>>();
	
	private static Map<Long,Map<String,Object>> resourceUrls = new HashMap<Long, Map<String,Object>>();

	@Autowired
	private ICmsResourceService cmsResourceService;
	
	public Map<String,Object> getResourceUrl(Long roleId) throws  Exception{
//		if(!jedis.exists(admin_role_id)){
//			IAdminPrivilegeService adminPrivilegeServiceImpl = (IAdminPrivilegeService)CustomBeanFactory.getContext(CustomBeanConst.ADMIN_SERVICE_PATHS).getBean("adminPrivilegeServiceImpl");
//			List<AdminPrivilege> lists = adminPrivilegeServiceImpl.findAdminPrivilegeListByAdminRoleId(admin_role_id);
//			if(null!=lists&&lists.size()>0){
//				Map<String,String> maps = new HashMap<String, String>();
//				for(AdminPrivilege adminPrivilege :lists){
//					maps.put(adminPrivilege.getUrl(),adminPrivilege.getId());
//				}
//				lists.clear();
//				lists = null;
//				jedis.hmset(admin_role_id, maps);
//				maps.clear();
//				maps = null;
//			}
//			adminPrivilegeServiceImpl = null;
//		}
//		jedis.hget(admin_role_id, field)
		if(!resourceUrls.containsKey(roleId)){
			List<CmsResource> lists = cmsResourceService.findList("roleId", roleId);
			if(null!=lists&&lists.size()>0){
				Map<String,Object> maps = new HashMap<String, Object>();
				for(CmsResource cmsResource :lists){
					maps.put(cmsResource.getUrl(),cmsResource.getId());
				}
				lists.clear();
				lists = null;
				resourceUrls.put(roleId, maps);
			}
		}
		return resourceUrls.get(roleId);
	}
	
	public Map<Long,Map<Long,CmsResource>> getResource(Long roleId) throws  Exception{
		if(!resources.containsKey(roleId)){
			List<CmsResource> lists = cmsResourceService.findList("roleId",roleId);
			if(null!=lists&&lists.size()>0){
				Map<Long,Map<Long, CmsResource>> userMaps = new HashMap<Long, Map<Long,CmsResource>>();
				Map<Long,CmsResource> maps = null;
				long parentId = 0l;
				for(CmsResource cmsResource :lists){

					parentId = cmsResource.getParentId();

					// 判断他的父URL是否存在  存在就新增操作 
					if(userMaps.containsKey(parentId)){
						userMaps.get(parentId).put((long)cmsResource.getId(), cmsResource);
					}else{
						maps = new HashMap<Long, CmsResource>();
						maps.put((long)cmsResource.getId(), cmsResource);
						userMaps.put(cmsResource.getParentId(), maps);
					}
				}
				lists.clear();
				lists = null;
				resources.put(roleId, userMaps);
			}
		}
		return resources.get(roleId);
	}
	
	public static void initResources(Long roleId) throws Exception{
		resources.remove(roleId);
		resourceUrls.remove(roleId);
		ResourcesUtil privilegeUtil = new ResourcesUtil();
		privilegeUtil.getResourceUrl(roleId);
		privilegeUtil.getResource(roleId);
	}
	
	public static void cleanResources() throws Exception{
		resources.clear();
		resourceUrls.clear();
	}
	
}
